/*
 * 
 * LegendShop 多用户商城系统
 * 
 *  版权所有,并保留所有权利。
 * 
 */
package com.legendshop.oa.dao.impl;

import com.legendshop.dao.impl.GenericDaoImpl;
import com.legendshop.dao.support.*;
import com.legendshop.dao.support.lambda.LambdaEntityCriterion;
import com.legendshop.framework.compare.DataListComparer;
import com.legendshop.oa.comparator.ProjectMemberComparator;
import com.legendshop.oa.constants.SysParameterEnum;
import com.legendshop.oa.dao.ConstTableDao;
import com.legendshop.oa.dao.ProjectMemberDao;
import com.legendshop.oa.model.ConstTable;
import com.legendshop.oa.model.Permission;
import com.legendshop.oa.model.ProjectMember;
import com.legendshop.oa.util.PropertiesUtil;
import com.legendshop.permission.common.PermissionComparator;
import com.legendshop.util.AppUtils;
import com.legendshop.dao.sql.ConfigCode;

import java.util.List;
import java.util.Map;

/**
 * The Class ProjectMemberDaoImpl. Dao实现类
 */

public class ProjectMemberDaoImpl extends GenericDaoImpl<ProjectMember, Long> implements ProjectMemberDao {
	
	private ConstTableDao constTableDao;

	/**
	 * 根据商城获取列表
	 */
	public List<ProjectMember> getProjectMember(String userName) {
		return this.queryByProperties(new EntityCriterion().eq("userName", userName));
	}

	/**
	 * 根据Id获取
	 */
	public ProjectMember getProjectMember(Long id) {
		String sqlStr = "SELECT pm.*, u.real_name as userRealName FROM ls_project_member pm, ls_admin_user u WHERE pm.user_name = u.name AND pm.id = ?";
		return get(sqlStr, ProjectMember.class, id);
	}

	/**
	 * 删除
	 */
	public int deleteProjectMember(ProjectMember projectMember) {
		return delete(projectMember);
	}

	/**
	 * 保存
	 */
	public Long saveProjectMember(ProjectMember projectMember) {
		if(projectMember.getRoleId() != null) {
			ConstTable constTable = constTableDao.getConstTablesBykey("STAFF_POSITION", projectMember.getRoleId().toString());
			if(constTable != null) {
				projectMember.setRoleName(constTable.getValue());
			}
		}
		
		String userNames = projectMember.getUserName(); //可以选择多个用户名
		String[] userNameStr = userNames.split(",");
		if(AppUtils.isNotBlank(userNameStr)) {
			for (String userName : userNameStr) {
				projectMember.setUserName(userName.trim());
				save(projectMember);
			}
		}
		
		return 1L;
	}

	@Override
	public Long saveProjectMember(Long projectId, List<ProjectMember> projectMember) {
		DataListComparer<ProjectMember, ProjectMember> comparer = new DataListComparer<ProjectMember, ProjectMember>(new ProjectMemberComparator());

		List<ProjectMember> dbList = this.queryByProperties(new LambdaEntityCriterion<ProjectMember>(ProjectMember.class).eq(ProjectMember::getProjectId, projectId));
		Map<Integer, List<ProjectMember>> compareResult = comparer.compare(projectMember,dbList, null);

		//不会有update 列表，直接更新
		List<ProjectMember> delList = compareResult.get(-1);
		List<ProjectMember> addList = compareResult.get(1);
		if(delList != null){
			delete(delList);
		}

		if(addList != null){
			//增加角色名字
			for (ProjectMember member : addList) {
				ConstTable constTable = constTableDao.getConstTablesBykey("STAFF_POSITION", member.getRoleId().toString());
				if(constTable != null) {
					member.setRoleName(constTable.getValue());
				}

			}

			save(addList);
		}
		return 1L;
	}

	/**
	 * 更新
	 */
	public int updateProjectMember(ProjectMember projectMember) {
		if(projectMember.getRoleId() != null) {
			ConstTable constTable = constTableDao.getConstTablesBykey("STAFF_POSITION", projectMember.getRoleId().toString());
			if(constTable != null) {
				projectMember.setRoleName(constTable.getValue());
			}
		}
		return update(projectMember);
	}

	/**
	 * 查询列表
	 */
	public PageSupport getProjectMember(CriteriaQuery cq) {
		return queryPage(cq);
	}

	@Override
	public PageSupport<ProjectMember> getProjectMemberByProject(Long projectId, String memberName, String curPageNO) {
		SimpleSqlQuery query = new SimpleSqlQuery(ProjectMember.class, curPageNO);		
		QueryMap map = new QueryMap();
		map.put("projectId",  projectId);
		map.like("memberName",  memberName);
		query.setPageSize(PropertiesUtil.getObject(SysParameterEnum.PAGE_SIZE, Integer.class));
		query.setParam(map.toArray());
		String querySql = ConfigCode.getInstance().getCode("oa.getProjectMember", map);
		String sqlCount = ConfigCode.getInstance().getCode("oa.getProjectMemberCount", map);
		query.setQueryString(querySql);
		query.setAllCountString(sqlCount);
		
		return querySimplePage(query);
	}

	public void setConstTableDao(ConstTableDao constTableDao) {
		this.constTableDao = constTableDao;
	}

	/**
	 * 检查用户是否在项目里
	 */
	@Override
	public boolean isMemberInProject(String username, Long projectId) {
		String sqlStr = "SELECT count(1) from ls_project_member where user_name = ? and project_id = ?";
		Integer result =  get(sqlStr, Integer.class, username, projectId);
		return result > 0;
	}

	@Override
	public List<ProjectMember> getProjectBatchMemberByProject(Long projectId) {
		return query("SELECT p.*, (SELECT real_name FROM ls_admin_user u WHERE u.name = p.user_name) userRealName FROM ls_project_member p WHERE p.project_id = ?",
				ProjectMember.class, projectId);
	}


}
